import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/ext.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/home/monitor/person_monitoring_page_entity_entity.dart';
import 'package:safetymonitor/router/app_pages.dart';

/// 最新人员监测
class NewPersonnelMonitoringController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  List<PersonMonitoringPageEntityEntity>? list = [];

  String startDate = '${DateTime.now().toStr.substring(0, 10)} 00:00';
  String endDate = '${DateTime.now().toStr.substring(0, 10)} 23:59';
  ///总数
  int mTotal = 0;

  /// 获取数据
  Future<List<PersonMonitoringPageEntityEntity>> theFeignEmergencyPlansPage(int index, int size, {String? search}) async {
    final Map<String, dynamic> queryParameters = {
      'pageNo': index,
      'pageSize': size,
      'enterTime':['$startDate:00','$endDate:00']
    };
    final res = await HttpUtil()
        .generateGetRequest<List<PersonMonitoringPageEntityEntity>>(
          Interface.realtimePersonMonitoring,
          queryParameters: queryParameters,
        )
        .execute();
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
     update();

    });
    return list ?? [];
  }

  @override
  onInit() {
    super.onInit();
    getTotal();
  }


  /// 获取数据
   getTotal() async {
    final Map<String, dynamic> queryParameters = {
      'pageNo': 1,
      'pageSize': 1,
      'enterTime':['${DateTime.now().toStr.substring(0, 10)} 00:00:00','${DateTime.now().toStr.substring(0, 10)} 23:59:00']
    };
    final res = await HttpUtil()
        .generateGetRequest<List<PersonMonitoringPageEntityEntity>>(
      Interface.realtimePersonMonitoring,
      queryParameters: queryParameters,
    )
        .execute();
    res.onSuccess((result) {
      mTotal= result.pagination?.total??0;
      update();
    });
  }


  void refreshPage() {
    globalKey.currentState?.onRefresh();
    getTotal();
  }

  //详情
  void onClickDetail(PersonMonitoringPageEntityEntity item) {
    Get.toNamed(Routes.personnelMonitoringDetails, arguments: item);
  }
}

extension WidgetX on Widget {
  Widget get wrapSized {
    return SizedBox(
      width: 137.w,
      child: this,
    );
  }
}
